﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsEmploymentStatusType
    {
        public string EmploymentStatus { get; set; }
        public int EmploymentStatusID { get; set; }

        public DataTable GetAllEmploymentsStatusTypes()
        {
            using (OleDataClient data = new OleDataClient())
            {
                return data.GetDataTable("Select * from EmploymentStatus", "EmploymentStatus");
            }
        }
        public void GetEmploymentStatusTypes(int employementStatusTypeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select * from EmploymentStatus where EmploymentStatusID=" + employementStatusTypeID.ToString(), "EmploymentStatus");
                if (dt.Rows.Count > 0)
                {
                    EmploymentStatusID = Convert.ToInt32(dt.Rows[0]["EmploymentStatusID"]);
                    EmploymentStatus = dt.Rows[0]["EmploymentStatus"].ToString();
                }
            }
        }
        public bool SaveEmploymentStatusType(string KeyForSave)
        {
            using (OleDataClient data = new OleDataClient())
            {
                if (KeyForSave == "add")
                {
                    if (EmploymentStatus == Utility.EmpStatus.ACTIVE.ToString() || EmploymentStatus == Utility.EmpStatus.RESIGNED.ToString())
                    {
                        throw new Exception("You cannot add 'ACTIVE' or 'RESIGNED' Employment Status.");
                    }
                    string count = data.GetValue("select count(*) from EmploymentStatus where EmploymentStatus='" + EmploymentStatus + "'");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Employment Status already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Insert into EmploymentStatus (EmploymentStatus) values('" + EmploymentStatus + "')");
                    }
                }
                else
                {
                    string status = data.GetValue("select EmploymentStatus from EmploymentStatus where EmploymentStatusID=" + EmploymentStatusID.ToString());
                    if (status == Utility.EmpStatus.ACTIVE.ToString() || status == Utility.EmpStatus.RESIGNED.ToString())
                    {
                        throw new Exception("You cannot modify 'ACTIVE' or 'RESIGNED' Employment Status.");
                    }

                    string count = data.GetValue("select count(*) from EmploymentStatus where EmploymentStatus='" + EmploymentStatus + "' and EmploymentStatusID NOT IN (" + EmploymentStatusID.ToString() + ")");
                    if (Convert.ToInt32(count) > 0)
                    {
                        throw new Exception("Employment Status Type already exist");
                    }
                    else
                    {
                        data.ExecuteNonQuery("Update EmploymentStatus set EmploymentStatus = '" + EmploymentStatus + "' where EmploymentStatusID=" + EmploymentStatusID.ToString());
                    }

                }
            }
            return true;
        }
        public void DeleteEmploymentStatusType()
        {
            using (OleDataClient data = new OleDataClient())
            {
                string status = data.GetValue("select EmploymentStatus from EmploymentStatus where EmploymentStatusID=" + EmploymentStatusID.ToString());
                if (status == Utility.EmpStatus.ACTIVE.ToString() || status == Utility.EmpStatus.RESIGNED.ToString())
                {
                    throw new Exception("You cannot delete 'ACTIVE' or 'RESIGNED' Employment Status.");
                }
                data.ExecuteNonQuery("Delete from  EmploymentStatus where EmploymentStatusID=" + EmploymentStatusID.ToString());
            }
        }
    }



}
